/**
 * Created by Administrator on 2017/1/11.
 */
$(function(){
    loading();
    $(".searchBtn").click(function () {
        if(isNotEmpty($(".searchContent").val())){
            var AppName = $(".searchContent").val();
            var cookie = new sweetCookie();
            cookie.setCookie("AppName", AppName);
            window.location.href = '/Lvscore_User_M/userWeb/views/myAD/applicationAD/search.html';
        }
    });
    ajaxGetUserAppList(1, "0");
    flushUser();
    $(".vedioAD").click(function () {
        //等待加载
        loading();
        $(".unwatchAD").empty();
        $(".picAD").removeClass("current");
        $(this).addClass("current");
        ajaxGetUserAppList(1,"0");
    });
    $(".picAD").click(function () {
        //等待加载
        loading();
        $(".unwatchAD").empty();
        $(".vedioAD").removeClass("current");
        $(this).addClass("current");
        ajaxGetUserAppList(1,"1");
    });
});
/**
 * 应用广告
 * @param page 当前页数
 * @param status
 * @returns
 */
function ajaxGetUserAppList(page, status) {
    var url="/Lvscore_User_M/myAds/ajaxGetUserAppList";
    $.post(url,{
        page : page,
        status : status
    }, function (data) {
        //$(".unwatchAD").empty();
        if(data.sumpage==0){
            $(".unwatchAD").html('<div class="noApplicationAD"><p class="notContent"><img src="/Lvscore_User_M/userWeb/static/images/nomessage.png" alt=""><p>您暂时没有添加应用广告，赶紧<a href="/Lvscore_User_M/userWeb/views/myAD/applicationAD/search.html">搜索</a>添加吧！</p></p></div>');
        }
        $.each(data.appList,function(index,element){
            //if(!isNotEmpty(element.app_logourl)){
            //    element.app_logourl="/Lvscore_User_M/userWeb/static/images/bm.png";
            //}
            $(".unwatchAD").append('<div class="item">'+
                '<div class="left">'+
                '<img src="'+element.app_logourl+'" alt="">'+
                '<p class="name handes" title="' + element.app_name + '">'+getTitle(element.app_name, 15, 3, 3)+'</p>'+
                '</div>'+
                '<ul class="left descript">'+
                '<li class="bindNO">绑定账号：'+ getTitle(element.bind_account, 10, 3, 3)+'</li>'+
                '<li>应用／服务换取积分：'+element.target_balance+'</li>'+
                '<li>已获取积分：'+element.alerady_balance+'</li>'+
                '</ul>'+
                '<a href="javascript:void(0)" id="watch_'+element.app_id+'" class="watch">'+playAgain(element.status)+'</a>'+
                '</div>');
            $("#watch_"+element.app_id).click(function(){
                var cookie=new sweetCookie();
                cookie.setCookie("appId",element.app_id);
                window.location.href="/Lvscore_User_M/userWeb/views/myAD/applicationAD/applicationAD.html";
            });
        });
        $(".loadingBox").hide();
        //分页
        if($(".tcdPageCode").html()==""){
            createPage(data.sumPage,data.nowpage, status);
        }
    },"json");
}
/**
 * 图片分页
 * @param pages
 * @param pageNum
 * @param status
 * @returns
 */
function createPage(pages,pageNum, status){
    if(pages>1){
        $(".tcdPageCode").createPage({
            pageCount:pages,
            current:pageNum,
            backFn:function(p){
                ajaxGetUserAppList(p, status);
            }
        });
    }
}

/**
 * 判断观看的状态
 * @param status
 * @returns
 */
function playAgain(status){
    if(status=="0"||!isNotEmpty(status)){
        return "观看";
    }else{
        return "再次观看";
    }
}


/**
 * 查找
 * @param e
 * @returns
 */
function searchEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13) {
        $(".searchBtn")[0].click();
    }
}